/**
 * web-upload 工具类
 *
 * 约定：
 * 上传按钮的id = 图片隐藏域id + 'BtnId'
 * 图片预览框的id = 图片隐藏域id + 'PreId'
 *
 * @author fengshuonan
 */
(function() {
    var PhotoUrlArray = [];
    var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../";
    var $IconUpload = function(pictureId) {
        var obj = $("#" + pictureId + "BtnId").parent();
        var typeName = $(obj).attr('id');
        var id = $("#id").val();
        var num = "111";
        if(id==null || id==''){
            id="aa";
            num = $("#num").val();
        }
        var cri= id+","+typeName +","+num;
        this.pictureId = pictureId;
        this.uploadBtnId = pictureId + "BtnId";
        this.uploadPreId = pictureId + "PreId";
        this.uploadUrl = applicationPath + '/caseSelectionUpload/upLoadProcess/'+cri;
        this.picWidth = 280;
        this.picHeight = 160;
    };

    $IconUpload.prototype = {
        /**
         * 初始化webUploader
         */
        initIcon : function() {
            var uploader = this.create();
            this.bindEvent(uploader);
            return uploader;
        },

        /**
         * 创建webuploader对象
         */
        create : function() {
            var webUploader = WebUploader.create({
                auto : true,
                pick : {
                    id : '#' + this.uploadBtnId,
                    multiple : true// 是否能上传多张
                },
                accept : {
                    title : 'Images',
                    extensions : 'gif,jpg,jpeg,bmp,png',
                    mimeTypes : 'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
                },
                swf: Feng.ctxPath + '${ctxPath}/static/css/plugins/webuploader/Uploader.swf',
                disableGlobalDnd : true,
                duplicate : true,
                server : this.uploadUrl
            });
            return webUploader;
        },
        /**
         * 绑定事件
         */
        bindEvent : function(bindObj) {
            var me =  this;
            /**
             * 当有文件添加进来的时候
             */
            bindObj.on('fileQueued', function(file) {
                // var $li = $(
                //     '<div id="' + file.id + '" class="cp_img" style="width: 300px;height: 200px;padding: 8px 9px;border: 1px solid #DDDDDD;">' +
                //     '<img>' +
                //     '<div class="cp_img_jian">删除</div></div>'
                //     ),
                //     $img = $li.find('img');
                // $("#" + me.uploadPreId).append($li);
                // bindObj.makeThumb(file, function(error, src) {
                //     if (error) {
                //         $img.replaceWith('<span>不能预览</span>');
                //         return;
                //     }
                //     $img.attr('src', src);
                // }, me.picWidth, me.picHeight);
            });
            /**
             * 文件上传过程中创建进度条实时显示
             */
            bindObj.on('uploadProgress', function(file, percentage) {
                var $li = $('#' + file.id),
                    $percent = $li.find('.progress span');
                // 避免重复创建
                if (!$percent.length) {
                    $percent = $('<p class="progress"><span></span></p>')
                        .appendTo($li)
                        .find('span');
                }
                $percent.css('width', percentage * 100 + '%');
            });
            /**
             * 文件上传成功，给item添加成功class, 用样式标记上传成功。
             */
            bindObj.on('uploadSuccess', function(file,response) {
                $('#' + file.id).addClass('upload-state-done');
                //将上传的url保存到数组
                // PhotoUrlArray.push(new PhotoUrl(response.id, response.filePath));
            });
            /**
             * 完成上传完了，成功或者失败，先删除进度条。
             */
            bindObj.on('uploadComplete', function(file) {
                $('#' + file.id).find('.progress').remove();
            });
            /**
             * 所有文件上传完毕
             */
            bindObj.on("uploadFinished", function () {
                var num =$("#num").val();
                var informationId =$("#id").val();
                var typeIcon = $("#" + me.uploadPreId).parent().attr('id');
                $.ajax({
                    url: Feng.ctxPath + "/caseSelectionUpload/getImg",
                    type: "post",
                    data: {
                        'num': num,
                        'informationId': informationId,
                        'typeIcon':typeIcon
                    },
                    success: function (data) {
                        $("#" + me.uploadPreId).html("");
                        for(var i=0 ; i<data.length ; i++){
                            var imgBox = $('<div id="'+data[i].id+'" class="cp_img" style="width: 300px;height: 200px;padding: 8px 9px;border: 1px solid #DDDDDD;"><img class="imagePath" src="'+data[i].imagePath+'" alt="" style="width: 280px;height: 160px;"><div class="cp_img_delete">删除</div></div>')
                            $("#" + me.uploadPreId).append(imgBox);
                        }
                    },
                    error: function (data) {
                        alert('没有权限，请联系管理员!');
                    }
                });
            });
            $(document).on('mouseover','.cp_img',function () {
                $(this).children(".cp_img_jian").css('display', 'block');
            });
            $(document).on('mouseout','.cp_img',function () {
                $(this).children(".cp_img_jian").css('display', 'none');
            });
            $("#" + me.uploadPreId).on("click", ".cp_img_jian", function () {
                var Id = $(this).parent().attr("id");
                //删除该图片
                bindObj.removeFile(bindObj.getFile(Id, true));
                $(this).parent().remove();
            });
        }
    };
    window.$IconUpload = $IconUpload;
}());
